package com.snailvr.tv.ui.view;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.snail.readyvr.R;
import com.snail.readyvr.util.SPUtils;
import com.snailvr.tv.Definition;
import com.snailvr.tv.ui.adapter.NavigationAdapter;
import com.snailvr.tv.ui.adapter.NavigationListItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MRKING on 2016/11/21.
 */

public class NavigationDialog {

    public static class Builder {
        private Context mContext;
        private View mContentView;
        private int mNavigationMode;
        private ListView mListView;
        private NavigationAdapter mNavigationAdapter;
        private AlertDialog mDialog;
        private View.OnClickListener mOnClickListener;
        List<NavigationListItem> items;


        public Builder(Context context) {
            mContext = context;
            mNavigationMode = (int) SPUtils.get(mContext, Definition.KEY_SNVR_NAVIGATION, Definition.SNVR_NAVIGATION_SENSOR);
            String[] nameList = context.getResources().getStringArray(R.array.nav_types);

            items = new ArrayList<NavigationListItem>();

            for (int i = 0; i < nameList.length; i++) {
                if (mNavigationMode == i) {
                    items.add(i, new NavigationListItem(nameList[i], true));
                } else {
                    items.add(i, new NavigationListItem(nameList[i],false));
                }
            }
        }

        public Builder addOnClickListener(View.OnClickListener listener) {
            mOnClickListener = listener;
            return this;
        }

        public AlertDialog show() {
            mNavigationAdapter = new NavigationAdapter(mContext, items, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    mDialog.dismiss();
                    if (mOnClickListener!=null)
                        mOnClickListener.onClick(v);
                }
            });

            mContentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_navigation_layout, null);

            mListView = (ListView) mContentView.findViewById(R.id.list_view_navigation);

            mListView.setAdapter(mNavigationAdapter);

            AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.SingleChoiceDialog);

            final AlertDialog dialog = builder
                    .setView(mContentView).show();
            mDialog = dialog;
            mDialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

            return dialog;

        }


    }
}
